package com.wcity.manage.service.community.social.impl;

import com.wcity.manage.common.enums.SaveImageType;
import com.wcity.manage.common.vo.community.social.VisitRecordVo;
import com.wcity.manage.dao.mapper.community.social.VisitRecordMapper;
import com.wcity.manage.dao.model.community.activity.Activity;
import com.wcity.manage.dao.model.community.image.ImageUrl;
import com.wcity.manage.dao.model.community.social.VisitRecord;
import com.wcity.manage.service.community.image.ImageService;
import com.wcity.manage.service.community.social.VisitRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社工走访记录
 * Created by hgg on 2016/12/21.
 */
@Service
public class VisitRecordServiceImpl implements VisitRecordService {

    @Autowired
    private VisitRecordMapper visitRecordMapper;
    @Autowired
    private ImageService imageService;

    /**
     * 社工走访记录列表
     * @param queryMap
     * @return
     */
    @Override
    public List<VisitRecordVo> visitRecordPageList( Map<String, Object> queryMap ) {

        return visitRecordMapper.visitRecordPageList( queryMap );
    }

    /**
     * 删除社工走访记录
     * @param deleteMap
     * @return
     */
    @Override
    public Integer deleteVisitRecord( Map<String, Object> deleteMap ) {

        return visitRecordMapper.deleteVisitRecord( deleteMap );
    }

    /**
     * 新增社工走访记录
     * @param visitRecord
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveAddVisitRecord( VisitRecord visitRecord,String imgUrl,String blockIdStr ) {

        //【1】生成父级社区活动
        Integer parentId = 0;
        String[] blockIds = blockIdStr.split( "," );
        visitRecord.setBlockId( Integer.parseInt( blockIds[0] ) );
        visitRecord.setPid( 0 );
        Integer saveNum = visitRecordMapper.saveAddVisitRecord( visitRecord );
        if( saveNum == 0 ){
            throw new RuntimeException( "新增社工走访记录失败" );
        }
        parentId = visitRecord.getId();

        //【2】保存关联封面图片
        ImageUrl image = new ImageUrl( null,imgUrl,null,parentId, SaveImageType.COMMUNITY_VISITRECORD );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存社工走访记录封面图片失败" );
        }

        //【3】执行保存社工走访记录
        saveAddVisitRecord( visitRecord, blockIds, parentId,imgUrl );
        return 1;
    }

    /**
     * 更新社工走访记录
     * @param visitRecord
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveUpdateVisitRecord( VisitRecord visitRecord,String imgUrl,String blockIdStr ) {

        Integer parentId = visitRecord.getId();
        String[] blockIds = blockIdStr.split( "," );

        //【1】删除以前社区活动封面
        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put( "referId", visitRecord.getId() );
        deleteMap.put( "saveType", SaveImageType.COMMUNITY_VISITRECORD );
        imageService.deleteImage( deleteMap );

        //【2】删除以前的小区概况
//        Map<String,Object> deleteVisitRecordMap = new HashMap<>();
//        deleteVisitRecordMap.put( "pid",visitRecord.getId() );
//        deleteVisitRecordMap.put( "userId",visitRecord.getUserId() );
//        Integer deleteNum = visitRecordMapper.deleteVisitRecord( deleteVisitRecordMap );
//        if( deleteNum == 0 ){
//            throw new RuntimeException( "删除社区活动失败" );
//        }

        //【3】执行保存社区活动封面
        ImageUrl image = new ImageUrl( null,imgUrl,null,visitRecord.getId(),SaveImageType.COMMUNITY_VISITRECORD );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存社区活动封面图片失败" );
        }

        //【4】执行更新社区活动概况
        visitRecord.setBlockId( Integer.parseInt( blockIds[0] ) );
        Integer updateNum = visitRecordMapper.saveUpdateVisitRecord( visitRecord );
        if( updateNum == 0 ){
            throw new RuntimeException( "更新社区活动失败 ");
        }

        //【5】执行新增社区活动
//        saveAddVisitRecord(visitRecord, blockIds, parentId,imgUrl);
        return 1;
    }

    /**
     * 社工走访记录详情
     * @param detailMap
     * @return
     */
    @Override
    public VisitRecordVo visitRecordDetail( Map<String, Object> detailMap ) {
        return visitRecordMapper.visitRecordDetail( detailMap );
    }

    /**
     * 社工走访记录总数量
     * @param queryMap
     * @return
     */
    @Override
    public Integer totalVisitRecord( Map<String, Object> queryMap ) {

        return visitRecordMapper.totalVisitRecord( queryMap );
    }

    /**
     * 社工走访记录列表非分页
     * @param queryMap
     * @return
     */
    @Override
    public List<VisitRecordVo> visitRecordList( Map<String, Object> queryMap ) {

        return visitRecordMapper.visitRecordList( queryMap );
    }

    /**
     * 查询小区ID拼接（1,2,3）
     * @param queryMap
     * @return
     */
    public String blockIds( Map<String,Object> queryMap ){

        return visitRecordMapper.blockIds( queryMap);
    }

    /**
     * 执行保存社工走访记录
     * @param visitRecord
     * @param blockIds
     * @param parentId
     */
    private void saveAddVisitRecord( VisitRecord visitRecord ,String[] blockIds,Integer parentId,String imgUrl ){

        for ( String id : blockIds ){
            visitRecord.setBlockId( Integer.parseInt( id ) );
            //【1】执行保存
            visitRecord.setPid( parentId );
            Integer saveNum = visitRecordMapper.saveAddVisitRecord( visitRecord );
            if( saveNum == 0 ){
                throw new RuntimeException("保存社工走访记录失败");
            }

            //【2】执行保存社区活动封面
            ImageUrl image = new ImageUrl( null,imgUrl,null,visitRecord.getId(),SaveImageType.COMMUNITY_VISITRECORD );
            Integer saveImageNum = imageService.saveAddImage( image );
            if( saveImageNum == 0 ){
                throw new RuntimeException( "保存社区活动封面图片失败" );
            }
        }
    }
}
